/// <summary>
/// Copyright 2010 Ivo Limmen (ivo@limmen.org)
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
/// 
/// http://www.apache.org/licenses/LICENSE-2.0
/// 
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// </summary>

using System;
using System.Runtime.Remoting;
using System.Collections.Generic;
using Services.Logging.Api;
using Services.Configuration.Api;

namespace ServiceController.Api
{	
	public class BaseService : MarshalByRefObject, IService
	{			
		private string name;
		
		private string description;
				
		private IServiceManager serviceManager;

		private ILogger logger;

        private IResolver configuration;

		public BaseService(string name, string description)
		{
			this.name = name;
			this.description = description;
		}
		
		#region IService implementation
		
		public virtual string[] GetRequiredServices() 
		{
			return null;
		}
		
		public string GetName ()
		{
			return this.name;
		}
		
		public string GetDescription ()
		{
			return this.description;
		}
	
		public virtual void SetServiceManager (IServiceManager serviceManager)
		{
			this.serviceManager = serviceManager;
		}		

		public virtual void SetLogger(ILogger logger)
		{
			this.logger = logger;
		}

        public virtual void SetConfiguration(IResolver resolver)
        {
            this.configuration = resolver;
        }

		public virtual bool RequiresRemoting() 
		{
			return true;
		}
		
		public virtual void Initialize() 
		{
		}

        public virtual void Stop()
        {
        }
		
		#endregion		
		
		protected IServiceManager GetServiceManager()
		{
			return this.serviceManager;
		}		
		
		protected IService GetService(string serviceName)
		{
			return this.GetServiceManager().GetServiceByName(serviceName);
		}

        protected string GetSetting(string key)
        {
            return this.configuration.GetSetting(null, GetName(), key);
        }

        protected IResolver GetLocalConfiguration()
        {
            return this.configuration;
        }

		protected ILogger GetLogger()
		{
			return this.logger;
		}

        public override int GetHashCode()
        {
            return GetName().GetHashCode() * 37;
        }

        public override bool Equals(object obj)
        {
            if (obj != null) 
            {
                if (obj is IService) 
                {
                    IService other = obj as IService;

                    if (other != null) 
                    {
                        return this.GetName().Equals(other.GetName());
                    }
                }
            }

            return false;
        }
	}
}
